/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package astroLib;

import record.*;
/**
 *
 * @author mgeden
 */
public class Settings implements Methods {

    public static LocationPrefer locationPrefer;
    public static CalculationPrefer calPref;
    public static OptionPrefer optionPref;
    public static WarningPrefer warningPref;

    public  Settings (LocationPrefer locationPrefer, CalculationPrefer calPref,OptionPrefer optionPref,WarningPrefer warningPref)
    {
         Settings.locationPrefer=locationPrefer;
         Settings.calPref=calPref;
         Settings.optionPref=optionPref;
         Settings.warningPref=warningPref;

    }

    public  static void getSalatTimes (double MJD0h, double[] salatTimes)

    {   
        double fajr,sunRise,zuhr,asr,asrHanefi,magrib,isha,riseSetAngle,sunRadius;
    
        EarthPosition position;
        SalatTimes times;
        position=new EarthPosition(Math.toRadians(locationPrefer.getLatitude()),Math.toRadians(locationPrefer.getLongitude()),
                locationPrefer.getAltitude(),locationPrefer.getTemperature(),locationPrefer.getPressure());
        times= new SalatTimes (position,MJD0h);

      
        fajr=times.getFajrTime(calPref.getDawnAngle(),calPref.getTemkinImsak());
        //riseSetAngle=-5.0/6.0-0.0347*Math.sqrt(position.getAltitude())
        sunRadius=16/60.0;
        riseSetAngle=-sunRadius-
                getAtmosphericRefraction(0)*getWeatherCorrectionCoefficent(position.getTemperature(),position.getPressure())
                -getAltitudeCorrection(position.getAltitude());

        sunRise=times.getSunRiseTime(riseSetAngle,calPref.getTemkinGunes());
        zuhr=times.getZuhrTime(calPref.getTemkinOgle());
        asrHanefi=times.getAsrHanefiTime(calPref.getTemkinAsr());
        if (calPref.getSectChoice()==0)
            asr=times.getAsrSafiTime(calPref.getTemkinAsr());
        else
            asr=asrHanefi;
        if (calPref.getCalculationMethod()==0)
           magrib=times.getMagribTime(riseSetAngle,calPref.getTemkinMagrib());
        else
        {
          magrib=times.getMagribTime((riseSetAngle),calPref.getTemkinMagrib());
        }
        isha=times.getIshaTime(calPref.getDuskAngle(),calPref.getTemkinYatsi());
        if (calPref.getCalculationMethod()==5) isha=magrib+1.5;

        switch (calPref.getEstimationMethod()) {
            case NO_ESTIMATION :
            break;
            case NEAREST_LATITUDE :
            break;
            case NEAREST_DAY :
            break;
            case AN_AMOUNT_OF_NIGHT:
            break;
            case MIDDLE_OF_NIGHT :
            break;
            }


        salatTimes[0]=fajr;
        salatTimes[1]=sunRise;
        salatTimes[2]=zuhr;
        salatTimes[3]=asr;
        salatTimes[4]=magrib;
        salatTimes[5]=isha;
        //return true;
    }
     public  static  void getKerahatTimes (double MJD0h, double[] kerahatTimes)

     {  EarthPosition position;
        TimesKerahat kerahat;
        position=new EarthPosition(Math.toRadians(locationPrefer.getLatitude()),Math.toRadians(locationPrefer.getLongitude()));
        kerahat= new TimesKerahat(position,MJD0h);
        kerahatTimes[0]= kerahat.getIsrakTime(10);
        kerahatTimes[1]= kerahat.getDuhaTime();
        kerahatTimes[2]= kerahat.getIstivaTime();
        kerahatTimes[3]= kerahat.getIsfirarTime();
        kerahatTimes[4]= kerahat.getAsrHanefiTime(calPref.getTemkinAsr());
     }
        /** Compute the  elevation  correction for sunset an sunrise,
         * @param	h altitude in meter from sea level
         * @result	corection for altitude angle in degrees
         *
         */
      private static  double getAltitudeCorrection(double h)
            {
                return 0.0347*Math.sqrt(h);
            }
    /* Compute the  Atmospheric Refraction in minutes at apperant angle in degrees,
     * R=1/(tan(ho+7.31/(ho+4.4)))+0.001351521723799;
     * R is the atmospheric refraction at the apperant altitude h0 degrees;
     * @param	angle ho is the apperant  altitude  for celestial object in degrees.
     * @result	corection for altitude angle in Minutes
     */
     private static double getAtmosphericRefraction(double ho)
            {
                double R=1/(Math.tan(Math.toRadians(ho+7.31/(ho+4.4))))+0.001351521723799;
                return R/60;
            }

    /**
     * Computes the refraction correction angle.
     * The effects of the atmosphere vary with atmospheric pressure, humidity
     * and other variables. Therefore the calculation is approximate. Errors
     * can be expected to increase the further away you are from the equator,
     * because the sun rises and sets at a very shallow angle. Small variations
     * in the atmosphere can have a larger effect.
     *
     * @param  zenith The sun zenith angle in degrees.
     * @return The refraction correction in degrees.
     */
    private static double refractionCorrection(final double zenith) {
        final double exoatmElevation = 90 - zenith;
        if (exoatmElevation > 85) {
            return 0;
        }
        final double refractionCorrection; // In minute of degrees
        final double te = Math.tan(Math.toRadians(exoatmElevation));
        if (exoatmElevation > 5.0) {
            refractionCorrection = 58.1/te - 0.07/(te*te*te) + 0.000086/(te*te*te*te*te);
        } else {
            if (exoatmElevation > -0.575) {
                refractionCorrection =  1735.0 + exoatmElevation *
                                       (-518.2 + exoatmElevation *
                                       ( 103.4 + exoatmElevation *
                                       (-12.79 + exoatmElevation *
                                         0.711)));
            } else {
                refractionCorrection = -20.774 / te;
            }
        }
        return refractionCorrection / 3600;
    }
    /* Compute the  Weather Correction Coefficent for given Temperature and Pressure,
     *    P     283.15
     * k=----- ------
     *    1010 273.15+T
     * k is the Correction Coefficent for athmospheric refraction which is R*k;
     * @param	T Temperature in Celcilus
     * @param	P Pressure in milliBars
     * @result	Weather Correction Coefficent unitless
     */
     private static double getWeatherCorrectionCoefficent(short T, short P)
            {

                return (P*283.15)/(1010.0*(273.15+T));
            }

}
